package com.crab.business.example.controller;

import com.crab.CrabApplication;
import com.crab.business.example.model.ExampleBean;
import com.crab.business.example.model.TestBean;
import com.crab.business.example.service.ExampleService;
import com.crab.framework.spring.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.util.List;

/**
 * Created by liusk on 2016/7/27.
 */
@Slf4j
@RestController
public class ExampleController {
    @Autowired
    private ExampleService exampleService;

    @RequestMapping(path = {"/json","/exampleForJson"},method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public List<ExampleBean> toJson(@RequestParam(name = "param", required = false, defaultValue = "刘书楷") String username) {
        log.info("i shall get bean :" + SpringContextUtils.applicationContext.getDisplayName());
        return exampleService.customMethod();
    }

    @RequestMapping(path = {"/xml","/exampleForXml"},method = RequestMethod.GET,produces = {"application/xml;charset=utf-8"})
    public List<ExampleBean> toXml(@RequestParam(name = "param", required = false, defaultValue = "刘书楷") String username) {
        exampleService.insert();
        return exampleService.findByName(username);
    }

    @RequestMapping(path = {"/test"}, method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public List<ExampleBean> test(@RequestParam(name = "param", required = false, defaultValue = "刘书楷") String username) {
        return exampleService.getList(username);
    }

    @RequestMapping(path = {"/login"}, method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public String login(@RequestParam(name = "param", required = false, defaultValue = "刘书楷") String username) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("liusk", "liusk");
        try {
            subject.login(token);
        } catch (UnknownAccountException uae) {
            //username wasn't in the system, show them an error message?
        } catch (IncorrectCredentialsException ice) {
            //password didn't match, try again?
        } catch (LockedAccountException lae) {
            //account for that username is locked - can't login.  Show them a message?
        } catch (AuthenticationException ae) {
            //unexpected condition - error?
        } catch (Exception e) {
            e.printStackTrace();
        }

        subject.checkRole("system");


        return String.valueOf(subject.isAuthenticated());
    }

    @RequestMapping(path = {"/index"}, method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public ModelAndView index() {
        return new ModelAndView("welcome");
    }

}
